package com.ithiem.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.dubbo.api.ReportApi;
import com.itheima.model.domain.Report;
import com.ithiem.dubbo.mappers.ReportMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class ReportApiImpl implements ReportApi {
    @Autowired
    private ReportMapper reportMapper;

    @Override
    public Long saveAndUpdate(Report report) {
        LambdaQueryWrapper<Report> rlqw = new LambdaQueryWrapper<>();
        rlqw.eq(Report::getUserId,report.getUserId()).eq(Report::getSoulLevel,report.getSoulLevel());
        Report report1 = reportMapper.selectOne(rlqw);
        if(report1 ==null){
            reportMapper.insert(report);//不存在就保存

        }else {
            report.setId(report1.getId());
            reportMapper.updateById(report);//存在就更新
        }
        return report.getId();
    }

    @Override
    public Report findByUserId(Long userId,String soulLevel) {
        LambdaQueryWrapper<Report> rlqw = new LambdaQueryWrapper<>();
        rlqw.eq(Report::getUserId,userId).eq(Report::getSoulLevel,soulLevel);
        return reportMapper.selectOne(rlqw);
    }

    @Override
    public Report findByReportId(Long id) {
        return reportMapper.selectById(id);
    }
}
